package dao;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import com.csvreader.CsvReader;

import connection.ConnectionManagerLibCVS;
import domain.Video;

public class LibCSVVideoDAO implements IVideoDAO {
	
	 CsvReader conn;	
	/**
	 * return all videos
	 */
	@Override
	public List<Video> getAllVideos() {
		List<Video> videos = new LinkedList<Video>();
		Video video = null;
		conn= ConnectionManagerLibCVS.getInstance("video.csv").openConnection();
		// Leemos los registros
		String [] valores;
		try {
			conn.readRecord();
			while (conn.readRecord()) {
				String valor = conn.getRawRecord();
				valores=valor.split(",");
				video = new Video(valores[1], new Integer(valores[2]), new Integer(valores[3]), valores[4], valores[5], valores[6], valores[7]);
				videos.add(video);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
		}		
		return videos;
	}
	
	/**
	 * return all videos with the specified parameter
	 */
	@Override
	public List<Video> getAllVideosByGenre(String genre) {
		List<Video> videos = new LinkedList<Video>();
		Video video = null;
		conn= ConnectionManagerLibCVS.getInstance("video.csv").openConnection();
		// Leemos los registros
		String [] valores;
		try {
			conn.readRecord();
			while (conn.readRecord()) {
				String valor = conn.getRawRecord();
				valores=valor.split(",");
				video = new Video(valores[1], new Integer(valores[2]), new Integer(valores[3]), valores[4], valores[5], valores[6], valores[7]);
				if(video.getGenre().equalsIgnoreCase(genre))
				{
					videos.add(video);
				}
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
		}		
		return videos;
	}
	
	/**
	 * return all videos starting with the specified letter
	 */
	@Override
	public List<Video> getAllVideosStartingWithLetter(char letter) {
		List<Video> videos = new LinkedList<Video>();
		Video video = null;
		conn= ConnectionManagerLibCVS.getInstance("video.csv").openConnection();
		// Leemos los registros
		String [] valores;
		try {
			conn.readRecord();
			while (conn.readRecord()) {
				String valor = conn.getRawRecord();
				valores=valor.split(",");
				video = new Video(valores[1], new Integer(valores[2]), new Integer(valores[3]), valores[4], valores[5], valores[6], valores[7]);
				if(video.getTitle().charAt(0)==letter)
				{
					videos.add(video);
				}
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
		}		
		return videos;
	}
	
	/**
	 * return the video with the specified title
	 */
	@Override
	public Video getVideoByTitle(String title) {
		Video video = null;
		conn= ConnectionManagerLibCVS.getInstance("video.csv").openConnection();
		// Leemos los registros
		String [] valores;
		try {
			conn.readRecord();
			while (conn.readRecord()) {
				String valor = conn.getRawRecord();
				valores=valor.split(",");
				video = new Video(valores[1], new Integer(valores[2]), new Integer(valores[3]), valores[4], valores[5], valores[6], valores[7]);
				if(video.getTitle().equalsIgnoreCase(title))
				{
					break;
				}
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();
			}
		}		
		return video;
	}
}
